﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using System.Xml;
using System.IO;

namespace FuturamaPDA {
    public partial class HomeScreen : Form {
		private TagReader reader;
		private Logger logger;
		private ScanningDialog scanningDialog;

		delegate void LogInDelegate(XmlTextReader reader);
		delegate void StopReadingDelegate(Boolean dispose);

        public HomeScreen() {
			InitializeComponent();
			this.reader = TagReader.Instance;
			this.logger = Logger.Instance;
			addHandlers();
        }

		private void readerLogin_Click(object sender, System.EventArgs e) {
			this.reader.startReading();
			this.scanningDialog = new ScanningDialog();
			if (this.scanningDialog.ShowDialog() == DialogResult.Cancel) {
				this.stopReading(false);
			}
		}

		private void stopReading(Boolean dispose) {
			this.Show();
			if (dispose) { this.scanningDialog.Dispose(); }
			this.reader.stopReading();
		}

		private void reader_RfidScanned(object sender, ReadEventArgs e) {
			StopReadingDelegate srd = new StopReadingDelegate(stopReading);
			this.Invoke(srd, true);

			Connector con = new Connector();
			con.Add("command", "getUserByTag");
			con.Add("id", e.Rfid);

			String xmlResponse = con.GetResponse();
			XmlTextReader reader = new XmlTextReader(new StringReader(xmlResponse));
			reader.WhitespaceHandling = WhitespaceHandling.None;

			LogInDelegate del = new LogInDelegate(logIn);
			this.Invoke(del, reader);
		}

		private void reader_ReaderError(object sender, ReaderErrorEventArgs e) {
			StopReadingDelegate srd = new StopReadingDelegate(stopReading);
			this.Invoke(srd, true);
			MessageBox.Show(e.ErrorMessage, "Foutmelding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
		}

		private void login_Click(object sender, System.EventArgs e) {
			if (this.usernameInput.Text != "" && this.passwordInput.Text != "") {
				Connector con = new Connector();
				con.Add("command", "getUser");
				con.Add("name", this.usernameInput.Text);
				con.Add("password", this.passwordInput.Text);

				String xmlResponse = con.GetResponse();
				XmlTextReader reader = new XmlTextReader(new StringReader(xmlResponse));
				reader.WhitespaceHandling = WhitespaceHandling.None;

				logIn(reader);
			}
		}

        private void exit_Click(object sender, System.EventArgs e) {
			Application.Exit();
        }

		private void logIn(XmlTextReader reader) {
			if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "users" && reader.Read()) {
				if (reader.Name == "user") {
					reader.Read();
					string userId = reader.ReadElementContentAsString();
					string name = reader.ReadElementContentAsString();
					string password = reader.ReadElementContentAsString();
					string listId = reader.ReadElementContentAsString();
					reader.Close();

					Form listScreen = new ListScreen(this, userId, name, listId);
					listScreen.Show();
					listScreen.Activate();
					this.Dispose(false);
				} else {
					MessageBox.Show("gebruikersnaam en/of wachtwoord zijn niet correct.");
					this.usernameInput.Text = "";
					this.passwordInput.Text = "";

				}
			} else {
				MessageBox.Show("Er is iets fout gegaan met het verbinden probeer het later opnieuw.");
			}
			reader.Close();
		}

		public void addHandlers() {
			this.reader.RfidScanned += new EventHandler<ReadEventArgs>(reader_RfidScanned);
			this.reader.ReaderError += new EventHandler<ReaderErrorEventArgs>(reader_ReaderError);
		}
    }
}